package com.apstest.core.domain;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileOutputStream;
import java.io.IOException;

public class TEST {
    public static void main(String[] args) {
        try {
            // 创建文档对象 (A4 大小)
            Document document = new Document(PageSize.A4);

            // 创建 PDF 写入器
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("VAT_Invoice.pdf"));
            document.open();

            // 设置中文字体
            BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            Font titleFont = new Font(baseFont, 16, Font.BOLD, BaseColor.BLACK);
            Font headerFont = new Font(baseFont, 10, Font.BOLD, BaseColor.BLACK);
            Font normalFont = new Font(baseFont, 10, Font.NORMAL, BaseColor.BLACK);
            Font smallFont = new Font(baseFont, 9, Font.NORMAL, BaseColor.BLACK);

            // 添加发票标题
            Paragraph invoiceTitle = new Paragraph("增值税发票样本", titleFont);
            invoiceTitle.setAlignment(Element.ALIGN_CENTER);
            invoiceTitle.setSpacingAfter(15);
            document.add(invoiceTitle);

            // 创建购买方表格 (3列)
            PdfPTable buyerTable = new PdfPTable(3);
            buyerTable.setWidthPercentage(100);
            buyerTable.setWidths(new float[]{1, 3, 2});

            // 第一行
            PdfPCell buyerLabel = new PdfPCell(new Phrase("购买方", headerFont));
            buyerLabel.setRowspan(4);
            buyerLabel.setHorizontalAlignment(Element.ALIGN_CENTER);
            buyerLabel.setVerticalAlignment(Element.ALIGN_MIDDLE);
            buyerTable.addCell(buyerLabel);

            PdfPCell nameLabel = new PdfPCell(new Phrase("名称:", headerFont));
            nameLabel.setBorderWidthRight(0);
            buyerTable.addCell(nameLabel);

            PdfPCell passwordArea = new PdfPCell(new Phrase("密码区", headerFont));
            passwordArea.setRowspan(4);
            passwordArea.setHorizontalAlignment(Element.ALIGN_CENTER);
            passwordArea.setVerticalAlignment(Element.ALIGN_MIDDLE);
            buyerTable.addCell(passwordArea);

            // 第二行
            PdfPCell taxIdLabel = new PdfPCell(new Phrase("纳税人识别号:", headerFont));
            taxIdLabel.setBorderWidthRight(0);
            buyerTable.addCell(taxIdLabel);

            // 第三行
            PdfPCell addressLabel = new PdfPCell(new Phrase("地址、电话:", headerFont));
            addressLabel.setBorderWidthRight(0);
            buyerTable.addCell(addressLabel);

            // 第四行
            PdfPCell bankLabel = new PdfPCell(new Phrase("开户行及账号:", headerFont));
            bankLabel.setBorderWidthRight(0);
            buyerTable.addCell(bankLabel);

            document.add(buyerTable);

            // 添加间距
            document.add(Chunk.NEWLINE);

            // 创建商品表格 (9列)
            PdfPTable productTable = new PdfPTable(9);
            productTable.setWidthPercentage(100);
            productTable.setWidths(new float[]{0.8f, 3f, 1.2f, 0.8f, 0.8f, 1f, 1f, 0.8f, 1f});

            // 表头
            addTableHeader(productTable, "货物或应税劳务、服务名称", headerFont);
            addTableHeader(productTable, "规格型号", headerFont);
            addTableHeader(productTable, "单位", headerFont);
            addTableHeader(productTable, "数量", headerFont);
            addTableHeader(productTable, "单价", headerFont);
            addTableHeader(productTable, "金额", headerFont);
            addTableHeader(productTable, "税率", headerFont);
            addTableHeader(productTable, "税额", headerFont);

            // 添加空行
            addEmptyRow(productTable, 9);
//            addEmptyRow(productTable, 9);
//            addEmptyRow(productTable, 9);
//            addEmptyRow(productTable, 9);
//            addEmptyRow(productTable, 9);

            // 合计行
            PdfPCell totalLabel = new PdfPCell(new Phrase("合计", headerFont));
            totalLabel.setColspan(2);
            totalLabel.setHorizontalAlignment(Element.ALIGN_CENTER);
            productTable.addCell(totalLabel);

            // 空单元格
            for (int i = 0; i < 7; i++) {
                PdfPCell emptyCell = new PdfPCell(new Phrase(""));
                productTable.addCell(emptyCell);
            }

            document.add(productTable);

            // 创建价税合计表格 (9列)
            PdfPTable taxTotalTable = new PdfPTable(9);
            taxTotalTable.setWidthPercentage(100);
            taxTotalTable.setWidths(new float[]{0.8f, 3f, 1.2f, 0.8f, 0.8f, 1f, 1f, 0.8f, 1f});

            PdfPCell taxTotalLabel = new PdfPCell(new Phrase("价税合计(大写)", headerFont));
            taxTotalLabel.setColspan(2);
            taxTotalTable.addCell(taxTotalLabel);

            PdfPCell taxTotalValue = new PdfPCell(new Phrase("(小写):", headerFont));
            taxTotalValue.setColspan(7);
            taxTotalTable.addCell(taxTotalValue);

            document.add(taxTotalTable);

            // 添加间距
            document.add(Chunk.NEWLINE);

            // 创建销售方表格 (3列)
            PdfPTable sellerTable = new PdfPTable(3);
            sellerTable.setWidthPercentage(100);
            sellerTable.setWidths(new float[]{1, 3, 2});

            // 第一行
            PdfPCell sellerLabel = new PdfPCell(new Phrase("销售方", headerFont));
            sellerLabel.setRowspan(4);
            sellerLabel.setHorizontalAlignment(Element.ALIGN_CENTER);
            sellerLabel.setVerticalAlignment(Element.ALIGN_MIDDLE);
            sellerTable.addCell(sellerLabel);

            PdfPCell sellerNameLabel = new PdfPCell(new Phrase("名称:", headerFont));
            sellerNameLabel.setBorderWidthRight(0);
            sellerTable.addCell(sellerNameLabel);

            PdfPCell remarkArea = new PdfPCell(new Phrase("备注", headerFont));
            remarkArea.setRowspan(4);
            remarkArea.setHorizontalAlignment(Element.ALIGN_CENTER);
            remarkArea.setVerticalAlignment(Element.ALIGN_MIDDLE);
            sellerTable.addCell(remarkArea);

            // 第二行
            PdfPCell sellerTaxIdLabel = new PdfPCell(new Phrase("纳税人识别号:", headerFont));
            sellerTaxIdLabel.setBorderWidthRight(0);
            sellerTable.addCell(sellerTaxIdLabel);

            // 第三行
            PdfPCell sellerAddressLabel = new PdfPCell(new Phrase("地址、电话:", headerFont));
            sellerAddressLabel.setBorderWidthRight(0);
            sellerTable.addCell(sellerAddressLabel);

            // 第四行
            PdfPCell sellerBankLabel = new PdfPCell(new Phrase("开户行及账号:", headerFont));
            sellerBankLabel.setBorderWidthRight(0);
            sellerTable.addCell(sellerBankLabel);

            document.add(sellerTable);

            // 创建签名表格 (4列)
            PdfPTable signatureTable = new PdfPTable(4);
            signatureTable.setWidthPercentage(100);
            signatureTable.setWidths(new float[]{1, 1, 1, 1});

            PdfPCell receiverLabel = new PdfPCell(new Phrase("收款人:", headerFont));
            signatureTable.addCell(receiverLabel);

            PdfPCell reviewerLabel = new PdfPCell(new Phrase("复核:", headerFont));
            signatureTable.addCell(reviewerLabel);

            PdfPCell issuerLabel = new PdfPCell(new Phrase("开票人:", headerFont));
            signatureTable.addCell(issuerLabel);

            PdfPCell sellerSignLabel = new PdfPCell(new Phrase("销售方:", headerFont));
            signatureTable.addCell(sellerSignLabel);

            document.add(signatureTable);

            // 添加发票说明
            Paragraph invoiceNote = new Paragraph("注：本发票为增值税专用发票样本，实际使用时请填写具体信息", smallFont);
            invoiceNote.setAlignment(Element.ALIGN_CENTER);
            invoiceNote.setSpacingBefore(20);
            document.add(invoiceNote);

            // 关闭文档
            document.close();

            System.out.println("增值税发票PDF生成成功: VAT_Invoice.pdf");

        } catch (DocumentException | IOException e) {
            e.printStackTrace();
        }
    }

    // 添加表头单元格
    private static void addTableHeader(PdfPTable table, String text, Font font) {
        PdfPCell cell = new PdfPCell(new Phrase(text, font));
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setPadding(5);
        table.addCell(cell);
    }

    // 添加空行
    private static void addEmptyRow(PdfPTable table, int columns) {
        for (int i = 0; i < columns; i++) {
            PdfPCell cell = new PdfPCell(new Phrase(""));
            cell.setFixedHeight(20);
            table.addCell(cell);
        }
    }
}
